package kuangsheng;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

//客户端
public class TcpClientDemo01 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            //1.地址和端口
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //创建一个socket连接
            socket = new Socket(inetAddress,port);
            //发送消息IO流
            outputStream = socket.getOutputStream();
            outputStream.write("你好。欢迎！".getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
